﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;
using WssEvents;

namespace JQMergeFramework
{
    public class InputManager : MonoBehaviour
    {
        private Touch theTouch;

        private GroundBase downGround;

        public static InputManager I;
        
        //是否禁止射线检测
        public bool isNoRayClick = false;

        private void Awake()
        {
            I = this;
        }


#if UNITY_GAME_TEST
        private void OnGUI()
        {
            if (GUI.Button(new Rect(new Vector2(100,100),new Vector2(200,100)),"<size=40>无人机</size>"))
            {
                GlobalEvent.Dispatcher(GameEvents.OnShowEvent,EventTypes.UAV);
            }
            
            if (GUI.Button(new Rect(new Vector2(100,200),new Vector2(200,100)),"<size=40>直升机</size>"))
            {
                GlobalEvent.Dispatcher(GameEvents.OnShowEvent,EventTypes.Helicopter);
            }
            
            if (GUI.Button(new Rect(new Vector2(100,300),new Vector2(200,100)),"<size=40>失败</size>"))
            {
                //没有可放置的位置
                if (DataManager.Instance.DayLimitHelicopter > 0)
                {
                    //没有可放置的位置
                    UIManager.Instance.GetWindow<UIOver>().Open();
                }
                else
                {
                    TowerControl.I.ShowPlaceTipsEft(false);
                    //失败动画
                    MergeManager.Instance.ClearRecord();
                }
            }
            
            if (GUI.Button(new Rect(new Vector2(100,400),new Vector2(200,100)),"<size=40>通关</size>"))
            {
                //通关
                MergeManager.Instance.GamePass();
            }
        }
#endif

        private void Update()
        {

            DataManager.Instance.CheckSave();
            MergeManager.Instance.Update();
            if (isNoRayClick || TowerControl.I.isPlayStartCityAnim)
            {
                return;
            }

            RunWindows();
// #if UNITY_EDITOR
//    
// #else
//             RunMobile();
// #endif
        }


        void RunWindows()
        {
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                //获取射线检测到的所有物体
                RaycastHit[] hits = Physics.RaycastAll(ray);
                
                foreach (var item in hits)
                {
                    GroundBase ground =  item.collider.GetComponent<GroundBase>();
                    if (ground != null)
                    {
                        downGround = ground;
                    }
                }
            }


            if (Input.GetMouseButtonUp(0))
            {

                //点击到UI。
                if (EventSystem.current.IsPointerOverGameObject())
                {
                    downGround = null;
                    return;
                }
                
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                //获取射线检测到的所有物体
                RaycastHit[] hits = Physics.RaycastAll(ray);
            
                //检测射线是否打到Place上
                GroundBase rayGround = null;

                foreach (var item in hits)
                {
              
                    UAVUnit uavUnit =  item.collider.GetComponent<UAVUnit>();
                    if (uavUnit!= null)
                    {
                        return;
                    }
                    
                    CopterUnit copterUnit =  item.collider.GetComponent<CopterUnit>();
                    if (copterUnit!= null)
                    {
                        return;
                    }
                    
                    GroundBase ground =  item.collider.GetComponent<GroundBase>();
                    if (ground != null)
                    {
                        rayGround = ground;
                    }
                }
                
                
                if (rayGround && downGround == rayGround)
                {
                    MergeManager manager = MergeManager.Instance;
        
                    if (MergeManager.Instance.isUseMoveProp)
                    {
                        //使用移动道具
                        if (manager.SaveOldBase!=null)
                        {
                            //移动到目标房子
                            manager.SaveClickMoveCity(null,rayGround);
                        }
                        else
                        {
                            if (!rayGround.isCanPlace())
                            {
                                //选择要移动的房子
                                manager.SaveClickMoveCity(rayGround,null);
                            }
                        }
                    }
                    else
                    {
                        if (rayGround.isCanPlace())
                        {
                            //放置
                            rayGround.OnRayClick();
                            AudioManager.Instance.Play(AudioName.PlaceCity);
                        }
                        else
                        {
                            
                            if (MergeManager.Instance.isUseRemoveProp)
                            {
                                //记录选择的道具
                                MergeManager.Instance.RemoveGroundCity(rayGround);
                            }
                        }
                    }
                    
                }

                downGround = null;
            }
        }

        void RunMobile()
        {
            //两只手触发时不做处理
            if (Input.touchCount == 1)
            {
                
                theTouch = Input.GetTouch(0);
                if (theTouch.phase == TouchPhase.Began)
                {
                    
                    Ray ray = Camera.main.ScreenPointToRay(theTouch.position);
                    //获取射线检测到的所有物体
                    RaycastHit[] hits = Physics.RaycastAll(ray);
                    
                    foreach (var item in hits)
                    {
                        GroundBase ground =  item.collider.GetComponent<GroundBase>();
                        if (ground != null)
                        {
                            downGround = ground;
                        }
                    }
                }
                
         
                if (theTouch.phase == TouchPhase.Ended)
                {
                    //点击到UI。
                    //点击到UI。
                    if (EventSystem.current.currentSelectedGameObject != null)
                    {
                        downGround = null;
                        return;
                    }
                
                    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    //获取射线检测到的所有物体
                    RaycastHit[] hits = Physics.RaycastAll(ray);
            
                    //检测射线是否打到Place上
                    GroundBase rayGround = null;

                    foreach (var item in hits)
                    {
                        GroundBase ground =  item.collider.GetComponent<GroundBase>();
                        if (ground != null)
                        {
                            rayGround = ground;
                        }
                    }
                
                    if (rayGround && downGround == rayGround)
                    {

                        MergeManager manager = MergeManager.Instance;
        
                        if (MergeManager.Instance.isUseMoveProp)
                        {
                            //使用移动道具
                            if (manager.SaveOldBase!=null)
                            {
                                //移动到目标房子
                                manager.SaveClickMoveCity(null,rayGround);
                            }
                            else
                            {
                                if (!rayGround.isCanPlace())
                                {
                                    //选择要移动的房子
                                    manager.SaveClickMoveCity(rayGround,null);
                                }
                            }
                        }
                        else
                        {
                            if (rayGround.isCanPlace())
                            {
                                //放置
                                rayGround.OnRayClick();
                                AudioManager.Instance.Play(AudioName.PlaceCity);
                            }
                            else
                            {
                            
                                if (MergeManager.Instance.isUseRemoveProp)
                                {
                                    //记录选择的道具
                                    MergeManager.Instance.RemoveGroundCity(rayGround);
                                }
                            }
                        }
                        
                    }

                    downGround = null;
                }

            }
        }

    }
}